import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 
 * @author Minh-Tuan Ta
 * This class store all of the configs of the server. This class support reading configs
 * from xml file.
 */
public class SimpleWebServerConfig {
	private int serverPort;
	private boolean isServerPortValid;
	private String documentRoot;
	private boolean isServerRootValid;
	private String[] imgTypes = {"gif", "jpg", "jpeg", "png"};
	
	public String[] getImageTypes()
	{
	  return imgTypes;
	}
	
	public int getServerPort()
	{
		return serverPort;
	}
	
	public String getDocumentRoot()
	{
		return documentRoot;
	}
	
	public boolean isServerPortValid()
	{
		return isServerPortValid;
	}
	
	public boolean isServerRootValid()
	{
		return isServerRootValid;
	}
	
	public boolean isConfigValid()
	{
		return isServerPortValid && isServerRootValid;
	}
	
	public String convertToServerPath(String fileName)
	{
	  return documentRoot + (fileName.charAt(0) == '/' || fileName.charAt(0) == '\\' ? fileName:"\\" + fileName);
	}
	
	public void loadConfigFromXML(String xmlFileName)
	{
		isServerPortValid = false;
		isServerRootValid = false;
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		try {

			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			//parse using builder to get DOM representation of the XML file
			Document dom = db.parse(xmlFileName);
			
			Element docEle = dom.getDocumentElement();
			
			//get the port number from the config file
			NodeList nl = docEle.getElementsByTagName("Port");
			
			if(nl != null && nl.getLength() > 0)
			{
				for(int i = 0 ; i < nl.getLength();i++)
				{

					//get the port element
					Element el = (Element)nl.item(i);

					//get the Employee object
					serverPort = Integer.parseInt(el.getFirstChild().getNodeValue());
					isServerPortValid = true;
				}
			}
			
			//get the server root from the config file
			nl = docEle.getElementsByTagName("ServerRoot");
			
			if(nl != null && nl.getLength() > 0)
			{
				for(int i = 0 ; i < nl.getLength();i++)
				{

					//get the port element
					Element el = (Element)nl.item(i);

					//get the Employee object
					File rootDir = new File(el.getFirstChild().getNodeValue());
					
					if (rootDir.exists())
					{
						documentRoot = rootDir.getAbsolutePath();
						isServerRootValid = true;
					}
					else
					{
						isServerRootValid = false;
					}
				}
			}
			
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	public String getErrorMessages()
	{
	  String msg = "";
	  
	  if (!isServerPortValid)
    {
      msg += "Server Port is not valid\n";
    }
    
    if (!isServerRootValid)
    {
      msg += "Server Root is not valid\n";
    }
    
    return msg;
	}
}
